import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginView.vue'),
    },
    {
      path: '/student',
      name: 'student-home',
      component: () => import('@/views/StudentHome.vue'),
      children: [
        { path: '', name: 'student-dashboard', component: { template: '<div></div>' } },
        { path: 'profile', name: 'student-profile', component: () => import('@/views/student/Profile.vue') },
        { path: 'positions', name: 'student-positions', component: () => import('@/views/student/Positions.vue') },
        { path: 'my-application', name: 'student-my-application', component: () => import('@/views/student/MyApplication.vue') },
        { path: 'my-position', name: 'student-my-position', component: () => import('@/views/student/MyPosition.vue') },
        { path: 'attendance', name: 'student-attendance', component: () => import('@/views/student/Attendance.vue') },
        { path: 'work-time-records', name: 'student-time-record', component: () => import('@/views/student/TimeRecord.vue') },
        { path: 'salary', name: 'student-salary', component: () => import('@/views/student/Salary.vue') },
        { path: 'feedback', name: 'student-feedback', component: () => import('@/views/student/Feedback.vue') },
      ]
    },
    {
      path: '/teacher',
      name: 'teacher-home',
      component: () => import('@/views/TeacherHome.vue'),
      children: [
        { path: '', name: 'teacher-dashboard', component: { template: '<div></div>' } },
        { path: 'profile', name: 'teacher-profile', component: () => import('@/views/teacher/Profile.vue') },
        { path: 'post', name: 'teacher-post', component: () => import('@/views/teacher/Post.vue') },
        { path: 'position-manage', name: 'teacher-position-manage', component: () => import('@/views/teacher/PositionManage.vue') },
        { path: 'approve', name: 'teacher-approve', component: () => import('@/views/teacher/Approve.vue') },
        { path: 'attendance', name: 'teacher-attendance', component: () => import('@/views/teacher/Attendance.vue') },
        { path: 'salary', name: 'teacher-salary', component: () => import('@/views/teacher/Salary.vue') },
        { path: 'stat', name: 'teacher-stat', component: () => import('@/views/teacher/Stat.vue') },
      ]
    },
    {
      path: '/admin',
      name: 'admin-home',
      component: () => import('@/views/AdminHome.vue'),
      children: [
        { path: '', name: 'admin-dashboard', component: { template: '<div></div>' } },
        { path: 'profile', name: 'admin-profile', component: () => import('@/views/admin/Profile.vue') },
        { path: 'user', name: 'admin-user', component: () => import('@/views/admin/User.vue') },
        { path: 'position', name: 'admin-position', component: () => import('@/views/admin/Position.vue') },
        { path: 'salary', name: 'admin-salary', component: () => import('@/views/admin/Salary.vue') },
        { path: 'report', name: 'admin-report', component: () => import('@/views/admin/Report.vue') },
      ]
    },
    {
      path: '/',
      redirect: '/login',
    },
  ],
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  // 自动恢复 pinia 状态
  if (!userStore.isLogin && localStorage.getItem('token')) {
    userStore.login(localStorage.getItem('role'))
  }
  // 需要登录的页面校验
  if (to.path !== '/login' && to.path !== '/register' && !localStorage.getItem('token')) {
    next('/login')
  } else {
    next()
  }
})

export default router
